#include "databaseipdialog.h"

DatabaseIpDalog::DatabaseIpDalog(QWidget *parent) :  QDialog(parent)
{
}

DatabaseIpDalog::~DatabaseIpDalog()
{
}


void DatabaseIpDalog::buildWidget(const QList<QString> &dbname,
                                  const QString        &def_dbname,
                                  const QString        &username,
                                  const QString        &userpswd,
                                  const QString        &addr,
                                  const int            &port)
{
  QSizePolicy              sizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
  int                      index;

  setWindowModality(Qt::WindowModal);
  resize(675, 175);
  setModal(true);

  verticalLayout = new QVBoxLayout(this);

  databaseGroupBox = new QGroupBox(this);
  databaseGroupBox->setLayoutDirection(Qt::LeftToRight);
  databaseGroupBox->setFlat(false);

  databaseVerticalLayout = new QVBoxLayout(databaseGroupBox);
  databaseVerticalLayout->setSpacing(5);
  databaseVerticalLayout->setContentsMargins(0, 0, 0, 0);

  databaseComboBox = new QComboBox(databaseGroupBox);

  for(index = 0; index < dbname.length(); index ++) {
    databaseComboBox->addItem(dbname.value(index));
  }

  if(def_dbname.length() > 0) {
    index = databaseComboBox->findText(def_dbname);
    databaseComboBox->setCurrentIndex(index);
  }else
    databaseComboBox->setCurrentIndex(0);


  databaseVerticalLayout->addWidget(databaseComboBox);

  databaseLoginHL = new QHBoxLayout();

  loginLabel = new QLabel(databaseGroupBox);

  databaseLoginHL->addWidget(loginLabel);

  loginLineEdit = new QLineEdit(databaseGroupBox);
  if(username.length() > 0) {
    loginLineEdit->setText(username);
  }


  databaseLoginHL->addWidget(loginLineEdit);

  passwordLabel = new QLabel(databaseGroupBox);

  databaseLoginHL->addWidget(passwordLabel);

  passwordLineEdit = new QLineEdit(databaseGroupBox);
  passwordLineEdit->setEchoMode(QLineEdit::Password);

  if(userpswd.length() > 0) {
    passwordLineEdit->setText(userpswd);
  }

  databaseLoginHL->addWidget(passwordLineEdit);

  databaseVerticalLayout->addLayout(databaseLoginHL);

  verticalLayout->addWidget(databaseGroupBox);

  dbAddressGroupBox = new QGroupBox(this);
  sizePolicy.setHorizontalStretch(0);
  sizePolicy.setVerticalStretch(0);
  sizePolicy.setHeightForWidth(dbAddressGroupBox->sizePolicy().hasHeightForWidth());
  dbAddressGroupBox->setSizePolicy(sizePolicy);

  horizontalLayout = new QHBoxLayout(dbAddressGroupBox);
  horizontalLayout->setSpacing(5);
  horizontalLayout->setContentsMargins(0, 0, 0, 0);

  addressLabel = new QLabel(dbAddressGroupBox);

  horizontalLayout->addWidget(addressLabel);

  addressLineEdit = new QLineEdit(dbAddressGroupBox);
  addressLineEdit->setMinimumSize(QSize(120, 0));

  if(addr.length() > 0) {
    addressLineEdit->setText(addr);
  }

  horizontalLayout->addWidget(addressLineEdit);

  addressPortHSpacer = new QSpacerItem(5, 20, QSizePolicy::Minimum, QSizePolicy::Minimum);

  horizontalLayout->addItem(addressPortHSpacer);

  portLabel = new QLabel(dbAddressGroupBox);

  horizontalLayout->addWidget(portLabel);

  portLineEdit = new QLineEdit(dbAddressGroupBox);
  sizePolicy.setHeightForWidth(portLineEdit->sizePolicy().hasHeightForWidth());
  portLineEdit->setSizePolicy(sizePolicy);
  portLineEdit->setMinimumSize(QSize(50, 0));

  if(port >= 0) {
    portLineEdit->setText(QString::number(port));
  }

  horizontalLayout->addWidget(portLineEdit);

  verticalLayout->addWidget(dbAddressGroupBox);

  buttonBox = new QDialogButtonBox(this);
  buttonBox->setOrientation(Qt::Horizontal);
  buttonBox->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);

  verticalLayout->addWidget(buttonBox);

/******************************************** RETRANSLATE UI ************************************************************************/
  this->setWindowTitle(QApplication::translate("Login_to_database_context", "Login to database", 0, QApplication::UnicodeUTF8));
  databaseGroupBox->setTitle(QApplication::translate("Login_to_database_context", "Database", 0, QApplication::UnicodeUTF8));
  loginLabel->setText(QApplication::translate("Login_to_database_context", "Login:", 0, QApplication::UnicodeUTF8));
  passwordLabel->setText(QApplication::translate("Login_to_database_context", "Password:", 0, QApplication::UnicodeUTF8));
  dbAddressGroupBox->setTitle(QApplication::translate("Login_to_database_context", "Database Address", 0, QApplication::UnicodeUTF8));
  addressLabel->setText(QApplication::translate("Login_to_database_context", "Addres:", 0, QApplication::UnicodeUTF8));
  portLabel->setText(QApplication::translate("Login_to_database_context", " : ", 0, QApplication::UnicodeUTF8));
/************************************************************************************************************************************/

  QObject::connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
  QObject::connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
}

void DatabaseIpDalog::accept()
{
  db_connect result;

  result.addr           = addressLineEdit->text();
  result.port           = portLineEdit->text().toInt();

  result.database_name  = databaseComboBox->currentText();

  result.database_login = loginLineEdit->text();
  result.database_pswd  = passwordLineEdit->text();


  QDialog::accept();
  emit dbloginaccept(result);
}

void DatabaseIpDalog::reject ()
{
  QDialog::reject();
}
